home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 033a / mxtms_10.zip / MAXTIME.C < prev    next >
C/C++ Source or Header  |  1991-05-27  |  12KB  |  351 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*    Maxtime.c    Version 1.0    By Craig Derouen                          */
  4. /*                 A Maximus CBBCS Timebank program                         */
  5. /*                                                                          */
  6. /*                                                                          */
  7. /****************************************************************************/
  8.  
  9. #define MAXTIME_MAIN
  10.  
  11. #include "maxtime.h"
  12. #include "globals.h"
  13.  
  14. int secret = FALSE;
  15.  
  16. void main(int argc,char **argv)
  17. {
  18.    int x;
  19.    int mtime,mbank;
  20.    unsigned y;
  21.    int tport=-1;
  22.    struct find_t fileinfo;
  23.    time_t thistime;
  24.  
  25.    exitcode = 0;
  26.    Task = 0;
  27.    MinutesWanted = 0;
  28.    mtime = 0;
  29.    mbank = 0;
  30.    SysopMode = FALSE;
  31.    UserBBS[0] = 0;
  32.  
  33.    strcpy(MCfgFile,"Maxtime.cfg");
  34.    strcpy(LastUse,"Lastuser.bbs");
  35.  
  36.  
  37.    for (x = 1; x < argc; x++) {        /* Parse out command line */
  38.       if (argv[x][0] == '-' || argv[x][0] == '/') {
  39.          y = toupper(argv[x][1]);        /* Argument switch */
  40.          switch (y) {
  41.             case '?':        /* Show switches only */
  42.                show_opts();
  43.                break;
  44.  
  45.             case 'L':
  46.                IsLocal = TRUE;
  47.                break;
  48.  
  49.             case 'P':        /* Specify port */
  50.                tport = argv[x][2] - '1';
  51.                break;
  52.  
  53.             case 'S':        /* Sysop mode */
  54.                SysopMode = TRUE;
  55.                IsLocal = TRUE;
  56.                break;
  57.  
  58.             case 'T':
  59.                Task = argv[x][2] - '0';
  60.                sprintf(LastUse,"Lastus%02d.bbs",Task);
  61.                break;
  62.  
  63.             case 'M':
  64.                y = toupper(argv[x][2]);        /* Argument switch */
  65.                if (y == 'T')
  66.                   mtime = atoi(argv[x]+3);
  67.                else if (y == 'B') mbank = atoi(argv[x]+3);
  68.                break;
  69.  
  70.             case 'Z':        /* Show parms */
  71.                secret = TRUE;
  72.                show_opts();
  73.                break;
  74.  
  75.          }
  76.       }
  77.       else {        /* Only non-switch argument specifies the MaxTime config file */
  78.          strcpy(MCfgFile,argv[x]);
  79.       }
  80.    }
  81.  
  82.    if (!SysopMode) {
  83.    /* Load in the Restart file data */
  84.       sprintf(temp,"Restar%02d.bbs",Task);
  85.       y = FALSE;
  86.       if(stat(temp,&fbuf) == 0 ) { 
  87.          Rstart = (struct _restart *) malloc(sizeof(struct _restart));
  88.          if (Rstart != NULL) {
  89.             x = sopen(temp,O_BINARY | O_RDWR,SH_DENYNO,S_IREAD);
  90.             if (x != -1) {
  91.                if(read(x,(char *)Rstart,sizeof(struct _restart)) == 0)
  92.                   y = TRUE;
  93.                close(x);
  94.                if (tport < 0)
  95.                   com_port = Rstart->port;
  96.                else com_port = tport;
  97.             }
  98.             else y = TRUE;
  99.          }
  100.          else y = TRUE;
  101.       }
  102.       else y = TRUE;
  103.       if(y) {
  104.          strout("Error reading Restart file, notify sysop!");
  105.          exit(1);
  106.       }
  107.  
  108.       if (com_port < 0 || com_port == 0x00ff)
  109.          IsLocal = TRUE;
  110.  
  111.       if (!IsLocal) {
  112.          if(!FossilInit())
  113.             aborterror(FILEOPEN,"Couldn't open FOSSIL");
  114.       }
  115.  
  116.    /* Compute how much time has the user been on already */
  117.  
  118.       init();        /* Start up things, read cfg file */
  119.  
  120.       if (mtime > 0)
  121.          MaxTimeSession = (unsigned) mtime;
  122.       if (mbank > 0)
  123.          MaxBankSession = (unsigned) mbank;
  124.  
  125.    /* Read in the lastuser.bbs file */
  126.  
  127.       x = sopen(LastUse,O_RDONLY | O_BINARY,SH_DENYNO,S_IREAD); 
  128.       if (x == -1) 
  129.          aborterror(FILEOPEN,"Missing Lastuse file");
  130.  
  131.       y = read(x,&LastUser,sizeof(struct _usr));
  132.       user_slen = (LastUser.struct_len ? LastUser.struct_len * 20 : 180);
  133.       lseek(x,0L,SEEK_SET);
  134.       y = read(x,&LastUser,user_slen);
  135.       if (y < (unsigned) user_slen) {        /* Humm couldnt read all of file! */
  136.          close(x);
  137.          aborterror(FILEOPEN,"Error reading LastUse file");
  138.       }
  139.       close(x);        /* We only need the file for the structure info */
  140.  
  141.    /* Compute BBStime which is amount of time already spent on bbs */
  142.       if( (x = (int) ((Rstart->timeoff - Rstart->timeon) / 60L) - LastUser.timeremaining) < 0)
  143.          x = 0;
  144.  
  145.       BBStime = (unsigned int) x;
  146.  
  147.       UserCfgnum = 0;        /* Startup */
  148.       newuser = FALSE;
  149.       if(stat(CfgFile,&fbuf) == 0 )         /* Does file exist? */
  150.          x = sopen(CfgFile,O_RDWR | O_BINARY,SH_DENYNO,S_IREAD);
  151.       else x = sopen(CfgFile,O_RDWR | O_BINARY | O_CREAT,SH_DENYNO,S_IWRITE);
  152.       if (x == -1)
  153.          aborterror(FILEOPEN,"Error opening/creating User config file");
  154.       UserCfgnum = find_config(x,LastUser.name,&USERCFG);
  155.       if (UserCfgnum < 0) {        /* Couldn't be found */
  156.          newuser = TRUE;
  157.          memset(&USERCFG,0,sizeof(struct user_cfg));
  158.          memcpy(USERCFG.name,LastUser.name,36);
  159.          UserCfgnum = find_blconfig(x);        /* Look for an empty slot */
  160.          if (UserCfgnum < 0)  /* Couldn't be found */
  161.             UserCfgnum = new_user(x);
  162.       }
  163.       close(x);        /* We only need the file for the structure info */
  164.       signon();
  165. /* Check and see is user has already been on here once today. */
  166.       time(&thistime);
  167.       if ((thistime - USERCFG.lasttime) < (time_t)(86400)) { /* within 24 hours? */
  168.          IsToday = TRUE;
  169.       }
  170.       else {
  171.          IsToday = FALSE;
  172.          USERCFG.dailydep = 0;
  173.          USERCFG.dailywtd = 0;
  174.       }
  175.       menu();
  176.    }
  177.  
  178.    else {        /* Special Sysop mode to do packing, maintainence */
  179.       init();        /* Start up things, read cfg file */
  180.  
  181.       sprintf(temp,"Begin, v%2.2f",Version);
  182.       logit(temp,'+');
  183.       strcpy(temp1,"Local sysop maintainence mode");
  184.       logit(temp1,':');
  185.       strout("MaxTime - a MAXIMUS timebank utility by Craig Derouen\r\n");
  186.       sprintf(temp,"\t *** Version %.2f ***\r\n",Version);
  187.       strout(temp);
  188.       strout(temp1);
  189.       strout("\n");
  190.  
  191. /* We still have to read the lastuse file, but just to get length for
  192.    later. So just look for ANY lastuser file. */
  193.  
  194.       if(_dos_findfirst("Lastus??.bbs",_A_NORMAL,&fileinfo) == 0) {
  195.          strcpy(LastUse,fileinfo.name);
  196.          x = sopen(LastUse,O_RDONLY | O_BINARY,SH_DENYNO,S_IREAD); 
  197.          if (x == -1) 
  198.             aborterror(FILEOPEN,"Missing Lastuse file");
  199.          read(x,&LastUser,sizeof(struct _usr));
  200.          user_slen = (LastUser.struct_len ? LastUser.struct_len * 20 : 180);
  201.          close(x);
  202.       }
  203.       else aborterror(FILEOPEN,"Missing Lastuse file");
  204.  
  205.       strcpy(LastUser.name,"Sysop");        /* Special mode */
  206.       Sys_menu();
  207.    }
  208.  
  209.    deinit(0);
  210. }
  211.  
  212. void menu(void)
  213. {
  214.    char resp;
  215.    int choice;
  216.    int good,x;
  217.  
  218.    choice = TRUE;
  219.    while (choice) {
  220.       setcolor(TextAttr[STD_TEXT]);
  221.       sprintf(temp,"\r\nYou have %lu minutes in the timebank currently.\r\n",USERCFG.banktime);
  222.       strout(temp);
  223.       strout("--------------------------------------------------------\r\n");
  224.       setcolor(TextAttr[MENU_KEY]);
  225.       strout("[A]dd time to your reserve  [W]ithdraw time to use today\r\n");
  226.       strout("[V]ersion                   [Q]uit back to Maximus\r\n");
  227.       strout("[S]tatistics\r\n");
  228.       if (LastUser.priv >= ASSTSYSOP) {
  229.          setcolor(TextAttr[STD_TEXT]);
  230.          strout("[U]ser stats (Sysop function)\r\n");
  231.       }
  232.       strout("\r\n");
  233.       good = FALSE;
  234.       while (!good) {
  235.          timeremain();
  236.          setcolor(TextAttr[PROMPT_TEXT]);
  237.          strout("Choice --> ");
  238.          x = chrin();
  239.          resp = (char) toupper(x);
  240.          if(resp == '\r')
  241.             resp = '!';
  242.          good = TRUE;
  243.          chrout(resp);
  244.          strout("\r\n");
  245.          setcolor(TextAttr[STD_TEXT]);
  246.          switch (resp) {
  247.             case 'A':        /* Add time to bank */
  248.                if(addtime())
  249.                   choice = FALSE;
  250.                break;
  251.  
  252.             case 'Q':
  253.             case 'G':
  254.                choice = FALSE;
  255.                break;
  256.  
  257.             case 'S':        /* User statistics */
  258.                setcolor(TextAttr[HILITE_TEXT]);
  259.                if (USERCFG.calls) {
  260.                   if (IsToday) 
  261.                      sprintf(temp,"\r\nYou have run MaxTime %d times, including at least once today.\r\n",USERCFG.calls);
  262.                   else sprintf(temp,"\r\nYou have run MaxTime %d times.\r\n",USERCFG.calls);
  263.                }
  264.                else sprintf(temp,"\r\nThis is your first run of MaxTime.\r\n");
  265.                strout(temp);
  266.                if (USERCFG.deposits) {
  267.                   sprintf(temp,"You have made %u deposits and %u withdrawals in MaxTime.\r\n",USERCFG.deposits,USERCFG.withdraws);
  268.                   strout(temp);
  269.                }
  270.                sprintf(temp,"You have been on the bbs for %u minutes.\r\n",BBStime);
  271.                strout(temp);
  272.                sprintf(temp,"You have been in MaxTime for %u minutes.\r\n",timeon());
  273.                strout(temp);
  274.                sprintf(temp,"You have %u minutes remaining on bbs.\r\n",LastUser.timeremaining - timeon());
  275.                strout(temp);
  276.                break;
  277.  
  278.             case 'U':
  279.                if (LastUser.priv >= ASSTSYSOP) 
  280.                   Sys_menu();
  281.                break;
  282.  
  283.             case 'V':
  284.                ShowVers();
  285.                break;
  286.  
  287.             case 'W':        /* Withdraw time from bank */
  288.                if(withdrawtime())
  289.                   choice = FALSE;
  290.                break;
  291.  
  292.             case '!':        /* Redraw the full menu */
  293.                good = TRUE;
  294.                break;
  295.  
  296.             default:
  297.                good = FALSE;
  298.                break;
  299.          }
  300.       }
  301.    }
  302. }
  303.  
  304. /* Display version and compiler time to user. Put this function here
  305.     in this module since TIMESTAMP checks only this module for date */
  306.  
  307. void ShowVers(void)
  308. {
  309.    struct tm *newtime;
  310.    time_t aclock;
  311.  
  312.    setcolor(TextAttr[STD_TEXT]);
  313.    sprintf(temp,"MaxTime, Version %.2f, By Craig Derouen\r\n",Version);
  314.    strout(temp);
  315.    setcolor(TextAttr[ATTN_TEXT]);
  316.    sprintf(temp,"Running under DOS %d.%d\r\n",_osmajor,_osminor);
  317.    strout(temp);
  318.    setcolor(TextAttr[HILITE_TEXT]);
  319.    sprintf(temp,"Program was last compiled: %s\r\n",__TIMESTAMP__);
  320.    strout(temp);
  321.    setcolor(TextAttr[MENU_KEY]);
  322.    sprintf(temp,"Remaining heap: %u bytes\r\n",_memavl());
  323.    strout(temp);
  324.    time(&aclock);
  325.    newtime = localtime(&aclock);
  326.    setcolor(TextAttr[STD_TEXT]);
  327.    sprintf(temp,"Current date and time are: %s\r\n",asctime(newtime));
  328.    strout(temp);
  329. }
  330.  
  331. void show_opts(void)
  332. {
  333.    printf("\nMaxTime Version: %2.2f by Craig Derouen\n",Version);
  334.    printf("Last compiled: %s\n",__TIMESTAMP__);
  335.    if (secret) {
  336.       printf("\t\a\aWaka! Waka! Waka! Author's secret function!\n");
  337.       printf("\tRemaining heap: %u bytes\n",_memavl());
  338.       printf("Since you are NOT the author, I am aborting...\n\n");
  339.    }
  340.    printf("Command line options:\n");
  341.    printf("\t /P: Specify com port (1=Com1,2=Com2,etc) Usually not needed\n");
  342.    printf("\t /S: Local Sysop maintainence mode\n");
  343.    printf("\t /T: Specify Task number. Tells Maxmail we are running multiuser\n");
  344.    printf("\t /MT: Specify maximum time user may borrow from bank\n");
  345.    printf("\t /MB: Specify maximum time user deposit in bank\n");
  346.    printf("\t {Filename} use this file as alt MaxTime.cfg file\n");
  347.    exit(0);
  348. }
  349.  
  350.  
  351.